package userInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import javax.swing.JTextArea;

public class TextOutput {

    private final static int SEPARATOR_LENGTH = 60;

    public final static String SEPARATOR = repeat("-", SEPARATOR_LENGTH);

    private static String repeat(String str, int multiple) {
        String returnStr = "";
        for (int i = 0; i < multiple; i++) {
            returnStr += str;
        }
        return returnStr;
    }
    
    private static ArrayList<String> messages = new ArrayList<String>();;

    private static JTextArea textArea;

    private boolean displayOutput;

    public TextOutput(boolean display) {
        this.displayOutput = display;
    }

    public static void setTextArea(JTextArea textArea){
        TextOutput.textArea = textArea;
    }

    public void setDisplay(boolean display){
        this.displayOutput = display;
    }
    
    public boolean getDisplay() {
        return displayOutput;
    }

    public ArrayList<String> getMessages() {
        return messages;
    }

    public String getLastMessage() {
        if (messages.isEmpty()) {
            return "";
        }

        return messages.get(messages.size() - 1);
    }

    private void println(String message) {
        messages.add(message);
        if (textArea != null) {
            textArea.append(message + '\n');
            textArea.updateUI();
        }
        System.out.println(message);
    }

    private void print(String message) {
        messages.add(message);
        if (textArea != null) {
            textArea.append(message);
            textArea.updateUI();
        }
        System.out.print(message);
    }

    /*
    private String convertToString(ArrayList<String> strList) {
        String str = "";
        for (String line : strList) {
            str += line + '\n';
        }
        return str;
    }
     *
     */

    public void println(Object message) {
        if (displayOutput) {
            println(message.toString());
        }
    }

    public void println() {
        if (displayOutput) {
            println(SEPARATOR);
        }
    }

    public void println(HashMap<?, ?> table) {
        if (displayOutput) {
            String line;
            for (Object k : table.keySet()) {
                line = k.toString() + ": " + table.get(k).toString();
                println(line);
            }
        }
    }

    public void print(Object o) {
        if (displayOutput) {
            print(o);
        }
    }

    public void print(String description, Object o) {
        if (displayOutput) {
            String line = description + ": " + o.toString();
            print(line);
        }
    }

    /**
     * Prints the content of an ArrayList<String>
     * 
     * @param strList
     *            the ArrayList<String> to be printed.
     */
    public void print(ArrayList<String> strList) {
        if (displayOutput) {
            String str = "[";

            for (String line : strList) {
                str += line + ", ";
            }

            str = str.substring(0, str.length() - 2) + "]";
            println(str);
        }

    }

    /**
     * Prints the content of an ArrayList<String>
     * 
     * @param strList
     *            the ArrayList<String> to be printed.
     */
    public void println(ArrayList<String> strList) {
        if (displayOutput) {
            for (String line : strList) {
                println(line);
            }
        }
    }

    public void print(String[] strList) {
        if (displayOutput) {
            if (strList.length == 0) {
                println("[]");
                return;
            }

            String str = "[";

            for (String line : strList) {
                str += line + ", ";
            }

            str = str.substring(0, str.length() - 2) + "]";
            println(str);
        }
    }

    public void print(HashMap<String, String> table) {
        if (displayOutput) {
            String line;
            for (String k : table.keySet()) {
                line = k + ": " + table.get(k);
                println(line);
            }
        }
    }

    /**
     * Given a HashMap<String, ArrayList<String>>, prints the HashMap in
     * this form:
     * key1
     * string1
     * string2
     * key2
     * string1
     * string2
     * string3
     * ...
     * 
     * @param dict
     *            a HashMap<String, ArrayList<String>> to be displayed onto
     *            the screen.
     */
    public void printDictionary(HashMap<String, ArrayList<String>> dict) {

        Set<String> keys = dict.keySet();

        for (String k : keys) {
            println(k);
            for (String element : dict.get(k)) {
                println("    " + element);
            }
        }
    }
}
